import java.awt.event.*;
import java.awt.*;

import javax.swing.*;

/**
 * Main window class for TriviaGame GUI.
 * @author Will
 *
 */
public class TriviaGameWindow extends JFrame implements ActionListener
{
	public static final int WINDOWWIDTH = 525;
	public static final int WINDOWHEIGHT = 365;
	
	Dimension buttonSize;
	Dimension tableSize;
	
	//JPanels
	private JPanel homePanel;
	private JPanel menuPanel;
	private JPanel categoryPanel;
	private JPanel playPanel;
	private JPanel friendsPanel;
	private JPanel scorePanel;
	
	//JLabels
	private JLabel dummyLabel;
	private JLabel titleLabel;
	private JLabel userNameLabel;
	private JLabel passwordLabel;
	private JLabel welcomeLabel;
	private JLabel categoryTitle;
	private JLabel questionLabel;
	private JLabel scoreTableTitle;
	private JLabel friendsTableTitle;
	
	//JButtons
	private JButton loginButton;
	private JButton aboutButton;
	private JButton exitButton;
	private JButton playButton;
	private JButton scoreButton;
	private JButton friendsButton;
	private JButton exitButtonMenu;
	private JButton exitButtonPlay;
	private JButton exitButtonCategory;
	private JButton exitButtonFriends;
	private JButton exitButtonScore;
	private JRadioButton answerButton0;
	private JRadioButton answerButton1;
	private JRadioButton answerButton2;
	private JRadioButton answerButton3;
	private ButtonGroup answerButtonGroup;
	private JRadioButton categoryButton;
	private ButtonGroup categoryButtonGroup;
	
	//JTextFields and JTextAreas
	private JTextField userNameTextField;
	private JPasswordField passwordTextField;
	
	// Data items
	private JTable scoreTable;
	String[] columnNamesScore = {"User", "Score", "Date"};
	Object[][] dataScore = 
		{
		     {"Ethan", "15", "4/13/2012"},
		     {"Nathan", "20", "4/13/2012"},
			 {"Will", "10", "4/13/2012"},
			 {"Jeremy", "25", "4/13/2012"},
		};
	private JTable friendsTable;
	String[] columnNamesFriends = {"Friends", "Score"};
	Object[][] dataFriends = 
		{
		     {"Ethan", "15"},
		     {"Nathan", "20"},
			 {"Will", "10"},
			 {"Jeremy", "25"},
		};
	JScrollPane scrollPaneScore;
	JScrollPane scrollPaneFriends;
	
	public TriviaGameWindow()
	{
		setTitle("Trivia Game");
		setSize(WINDOWWIDTH, WINDOWHEIGHT);
		//setResizable(false);
		setLocation(100, 100);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		buttonSize = new Dimension(80, 25);
		tableSize = new Dimension(200, 200);
		
		// Initialize the panels
		//setLayout(new GridBagLayout());
		homePanel = new JPanel(new GridBagLayout());
		menuPanel = new JPanel(new GridBagLayout());
		categoryPanel = new JPanel(new GridBagLayout());
		playPanel = new JPanel(new GridBagLayout());
		friendsPanel = new JPanel(new GridBagLayout());
		scorePanel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
	// Initialize and add the components to homePanel
		/*
		numberTextField = new JTextField(30);
		setDisplay("");
		numberTextField.setHorizontalAlignment(JTextField.RIGHT);
		numberTextField.setCaretPosition(numberTextField.getDocument().getLength());
		numberTextField.addActionListener(new NumberTextFieldHandler());
		*/
		
		// Title
		c.gridwidth = 4;
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(5, 10, 5, 5);
		c.weightx = 0.0;
		c.weighty = 0.0;
		//c.anchor = GridBagConstraints.FIRST_LINE_START;
		titleLabel = new JLabel("Trivia Game");
		//titleLabel.setFont(new Font(titleLabel.getFont().getName(),Font.BOLD,titleLabel.getFont().getSize()));
		titleLabel.setFont(new Font(titleLabel.getFont().getName(), Font.PLAIN, 36));
		homePanel.add(titleLabel, c);
		c.gridwidth = 1;
		
		// login controls
		c.gridx = 1;
		c.gridy = 3;
		c.insets = new Insets(75, 20, 0, 0);
		c.weightx = 0.0;
		c.weighty = 0.0;
		userNameLabel = new JLabel("Name");
		userNameLabel.setAlignmentX(RIGHT_ALIGNMENT);
		passwordLabel = new JLabel("Password");
		passwordLabel.setAlignmentX(RIGHT_ALIGNMENT);
		userNameTextField = new JTextField(10);
		passwordTextField = new JPasswordField(10);
		c.anchor = GridBagConstraints.LINE_END;
		homePanel.add(userNameLabel, c);
		c.gridx = 2;
		c.gridwidth = 2;
		c.anchor = GridBagConstraints.LINE_START;
		homePanel.add(userNameTextField, c);
		c.insets = new Insets(75, 5, 0, 0);
		c.gridwidth = 1;
		c.gridx = 1;
		c.gridy = 4;
		c.insets = new Insets(10, 20, 0, 0);
		homePanel.add(passwordLabel, c);
		c.gridx = 2;
		c.gridwidth = 2;
		homePanel.add(passwordTextField, c);
		c.ipadx = 0;
		c.gridwidth = 1;
		
		
		// loginButton
		c.gridx = 2;
		c.gridy = 6;
		c.insets = new Insets(20, 20, 0, 0);
		c.anchor = GridBagConstraints.LINE_START;
		//c.weightx = 0.5;
		//c.weighty = 0.5;
		loginButton = new JButton("Log in");
		loginButton.setPreferredSize(buttonSize);
		loginButton.addActionListener(this);
		homePanel.add(loginButton, c);
		c.anchor = GridBagConstraints.CENTER;
		
		// exitButton
		c.gridx = 3;
		c.gridy = 6;
		c.insets = new Insets(20, 10, 0, 0);
		c.anchor = GridBagConstraints.LINE_START;
		//c.weightx = 0.5;
		//c.weighty = 0.5;
		exitButton = new JButton("Exit");
		exitButton.setPreferredSize(buttonSize);
		exitButton.addActionListener(this);
		homePanel.add(exitButton, c);
		c.anchor = GridBagConstraints.CENTER;
		
		// aboutButton
		c.gridx = 2;
		c.gridy = 7;
		c.insets = new Insets(10, 20, 0, 0);
		c.anchor = GridBagConstraints.LINE_START;
		aboutButton = new JButton("About");
		aboutButton.setPreferredSize(buttonSize);
		aboutButton.addActionListener(this);
		homePanel.add(aboutButton, c);
		c.anchor = GridBagConstraints.CENTER;
		
		// align
		c.gridx = 5;
		c.gridy = 8;
		c.weightx = 0.5;
		c.weighty = 0.5;
		dummyLabel = new JLabel();
		homePanel.add(dummyLabel, c);
		
	
		add(homePanel);
		
	// Initialize and add the components to menuPanel	
		// welcomeLabel
		c.gridwidth = 1;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0.0;
		c.weighty = 0.0;
		c.insets = new Insets(10, 10, 0, 0);
		welcomeLabel = new JLabel("Welcome " + userNameTextField.getText() + ".");
		menuPanel.add(welcomeLabel, c);
		
		// Initialize the data table titles
		c.gridx = 0;
		c.gridy = 1;
		c.insets = new Insets(20, 0, 0, 0);
		scoreTableTitle = new JLabel("Scores");
		menuPanel.add(scoreTableTitle, c);
		c.gridx = 2;
		c.gridy = 1;
		friendsTableTitle = new JLabel("Friends");
		menuPanel.add(friendsTableTitle, c);
		
		
		// Initialize the data tables
		c.gridwidth = 2;
		c.gridx = 0;
		c.gridy = 2;
		c.insets = new Insets(0, 10, 0, 0);
		//c.weightx = 0.5;
		//c.weighty = 0.5;
		scoreTable = new JTable(dataScore, columnNamesScore);
		scoreTable.setBackground(this.getBackground());
		scoreTable.setGridColor(scoreTable.getBackground());
		friendsTable = new JTable(dataFriends, columnNamesFriends);
		friendsTable.setBackground(this.getBackground());
		friendsTable.setGridColor(scoreTable.getBackground());
		scrollPaneScore = new JScrollPane(scoreTable);
		scrollPaneFriends = new JScrollPane(friendsTable);
		scrollPaneScore.setPreferredSize(tableSize);
		scrollPaneFriends.setPreferredSize(tableSize);
		//scoreTable.setFillsViewportHeight(true);
		//friendsTable.setFillsViewportHeight(true);
		menuPanel.add(scrollPaneScore, c);
		//menuPanel.add(scoreTable, c);
		c.gridx = 2;
		c.gridy = 2;
		menuPanel.add(scrollPaneFriends, c);
		//menuPanel.add(friendsTable, c);
		c.weightx = 0.0;
		c.weighty = 0.0;
		c.gridwidth = 1;

		
		
		
		// playButton
		c.gridx = 2;
		c.gridy = 3;
		c.weightx = 0.0;
		c.weighty = 0.0;
		c.insets = new Insets(15, 10, 0, 0);
		playButton = new JButton("Play");
		playButton.setPreferredSize(buttonSize);
		playButton.addActionListener(this);
		menuPanel.add(playButton, c);
		
		/*
		// scoreButton
		c.gridx = 1;
		c.gridy = 3;
		//c.weightx = 0.5;
		//c.weighty = 0.5;
		scoreButton = new JButton("Scores");
		scoreButton.setPreferredSize(buttonSize);
		scoreButton.addActionListener(this);
		//menuPanel.add(scoreButton, c);
		
		// friendsButton
		c.gridx = 2;
		c.gridy = 3;
		friendsButton = new JButton("Friends");
		friendsButton.setPreferredSize(buttonSize);
		friendsButton.addActionListener(this);
		//menuPanel.add(friendsButton, c);
		*/
		
		// exitButtonMenu
		c.gridx = 3;
		c.gridy = 3;
		exitButtonMenu = new JButton("Log out");
		exitButtonMenu.setPreferredSize(buttonSize);
		exitButtonMenu.setActionCommand("home");
		exitButtonMenu.addActionListener(this);
		menuPanel.add(exitButtonMenu, c);
		c.weightx = 0.0;
		c.weighty = 0.0;

		add(menuPanel);
		
	// Initialize and add the components to categoryPanel
		categoryButtonGroup = new ButtonGroup();
		c.gridwidth = 1;
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(20, 20, 5, 5);
		categoryTitle = new JLabel("Choose a category:");
		categoryPanel.add(categoryTitle, c);
		c.gridx = 0;
		c.gridy = 2;
		c.insets = new Insets(10, 5, 0, 0);
		categoryButton = new JRadioButton("Science");
		categoryButton.addActionListener(this);
		categoryPanel.add(categoryButton, c);
		categoryButtonGroup.add(categoryButton);
		c.gridy++;
		c.insets = new Insets(0, 0, 0, 0);
		categoryButton = new JRadioButton("History");
		categoryButton.addActionListener(this);
		categoryPanel.add(categoryButton, c);
		categoryButtonGroup.add(categoryButton);
		c.gridy++;
		categoryButton = new JRadioButton("Sports");
		categoryButton.addActionListener(this);
		categoryPanel.add(categoryButton, c);
		categoryButtonGroup.add(categoryButton);
		c.gridx = 3;
		c.gridy = 6;
		c.weightx = 0.5;
		c.weighty = 0.5;
		
		categoryButtonGroup.add(categoryButton);
		exitButtonCategory = new JButton("Exit");
		exitButtonCategory.setPreferredSize(buttonSize);
		exitButtonCategory.setActionCommand("menu");
		exitButtonCategory.addActionListener(this);
		categoryPanel.add(exitButtonCategory, c);

		add(categoryPanel);
		
	// Initialize and add the components to playPanel	
		// questionLabel
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(5, 10, 20, 0);
		c.weightx = 0.0;
		c.weighty = 0.0;
		questionLabel = new JLabel("Bronze is made from copper and what other element?");
		playPanel.add(questionLabel, c);
		
		// answerButtons
		c.gridy = 1;
		c.insets = new Insets(0, 20, 0, 0);
		c.anchor = GridBagConstraints.FIRST_LINE_START;
		answerButtonGroup = new ButtonGroup();
		answerButton0 = new JRadioButton("Nickle");
		playPanel.add(answerButton0, c);
		c.gridy = 2;
		answerButton1 = new JRadioButton("Tin");
		playPanel.add(answerButton1, c);
		c.gridy = 3;
		answerButton2 = new JRadioButton("Iron");
		playPanel.add(answerButton2, c);
		c.gridy = 4;
		answerButton3 = new JRadioButton("Steel");
		playPanel.add(answerButton3, c);
		answerButtonGroup.add(answerButton0);
		answerButtonGroup.add(answerButton1);
		answerButtonGroup.add(answerButton2);
		answerButtonGroup.add(answerButton3);
		answerButton0.setSelected(false);
		answerButton1.setSelected(false);
		answerButton2.setSelected(false);
		answerButton3.setSelected(false);
		
		c.anchor = GridBagConstraints.CENTER;
		
		
		// exitButtonPlay
		c.gridwidth = 1;
		c.gridx = 0;
		c.gridy = 6;
		c.insets = new Insets(75, 10, 5, 5);
		exitButtonPlay = new JButton("Exit");
		exitButtonPlay.setPreferredSize(buttonSize);
		exitButtonPlay.setActionCommand("menu");
		exitButtonPlay.addActionListener(this);
		playPanel.add(exitButtonPlay, c);

		add(playPanel);
		
	// Initialize and add the components to scorePanel
		c.gridwidth = 1;
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(5, 10, 5, 5);
		exitButtonScore = new JButton("Exit");
		exitButtonScore.setPreferredSize(buttonSize);
		exitButtonScore.setActionCommand("menu");
		exitButtonScore.addActionListener(this);
		scorePanel.add(exitButtonScore, c);

		add(scorePanel);
		
	// Initialize and add the components to friendsPanel
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(5, 10, 5, 5);
		exitButtonFriends = new JButton("Exit");
		exitButtonFriends.setPreferredSize(buttonSize);
		exitButtonFriends.setActionCommand("menu");
		exitButtonFriends.addActionListener(this);
		friendsPanel.add(exitButtonFriends, c);

		add(friendsPanel);
		
		
		
		
		this.setContentPane(homePanel);
		this.revalidate();
		
	}
	
	
	/**
	 * Inherited abstract method from ActionListener.  The main event handler.
	 * @param e the event fired
	 * {@inheritDoc}
	 */
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("Log in"))
		{
			if(userNameTextField.getText().equals(""))
			{
				JOptionPane.showMessageDialog(this, "Enter your user name.");
				return;
			}
			welcomeLabel.setText("Welcome " + userNameTextField.getText() + ".");
			this.setContentPane(menuPanel);
			this.revalidate();
		}
		else if(e.getActionCommand().equals("Scores"))
		{
			this.setContentPane(scorePanel);
			this.revalidate();
		}
		else if(e.getActionCommand().equals("Exit"))
		{
			System.exit(0);
		}
		else if(e.getActionCommand().equals("About"))
		{
			String s = "Trivia Game\n\nCreated for CSE385 by:\n" + 
					   "   William Agnew\n" + 
					   "   Ethan Headings\n" + 
					   "   Nathan Kanta\n" + 
					   "   Jeremy Siegle\n\n";
			JOptionPane.showMessageDialog(this, s);
		}
		else if(e.getActionCommand().equals("menu"))
		{
			this.setContentPane(menuPanel);
			this.revalidate();
		}
		else if(e.getActionCommand().equals("home"))
		{
			// reset login text
			userNameTextField.setText("");
			passwordTextField.setText("");
			
			this.setContentPane(homePanel);
			this.revalidate();
		}
		else if(e.getActionCommand().equals("Play"))
		{
			this.setContentPane(categoryPanel);
			this.revalidate();
		}
		else if(e.getActionCommand().equals("Friends"))
		{
			this.setContentPane(friendsPanel);
			this.revalidate();
		}
		else if(e.getActionCommand().equals("Science") || e.getActionCommand().equals("History") || e.getActionCommand().equals("Sports"))
		{
			this.setContentPane(playPanel);
			this.revalidate();
		}
	}
}
